package com.example.billbillbangumi;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Message extends AppCompatActivity {
    private MyDatabaseHelper dbhelper;
    String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        Intent intent=getIntent();
        name=intent.getStringExtra("name");
        TextView message=(TextView)findViewById(R.id.message);
        dbhelper=new MyDatabaseHelper(this,"Bangumi.db",null,12);
        SQLiteDatabase db=dbhelper.getWritableDatabase();
        Cursor cursor=db.query("bangumi",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            do{
                String bname=cursor.getString(cursor.getColumnIndex("name"));
                String stime=cursor.getString(cursor.getColumnIndex("starttime"));
                String utime=cursor.getString(cursor.getColumnIndex("updatetime"));
                if(bname.equals(name)){
                    message.setText("番名："+bname+"\n开播时间："+stime+"\n更新时间："+utime);
                }
            }while (cursor.moveToNext());
        }
        cursor.close();
        Button attention=(Button)findViewById(R.id.attention);
        Button unattention=(Button)findViewById(R.id.unattention);
        Button pv=(Button)findViewById(R.id.pv);
        attention.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                SQLiteDatabase db=dbhelper.getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("attention","已追番");
                db.update("bangumi",values,"name=? and attention=?",new String[]{name,"未追番"});
                Toast.makeText(Message.this,"自己追的番就要好好看完哟^0^",Toast.LENGTH_SHORT).show();
            }
        });
        unattention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db=dbhelper.getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("attention","未追番");
                db.update("bangumi",values,"name=? and attention=?",new String[]{name,"已追番"});
                Toast.makeText(Message.this,"哼！居然真的抛弃了人家（> <)",Toast.LENGTH_SHORT).show();
            }
        });
        pv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Message.this,Video.class);
                intent.putExtra("name",name);
                startActivity(intent);
            }
        });

    }
}
